import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/login.vue');
const index = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/index');
const welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/welcome');

const users = () => import(/* webpackChunkName:"users_rights_roles" */ '../views/users');
const rights = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/power/rights');
const roles = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/power/roles');

const cate = () => import(/* webpackChunkName:"cate_params" */ '../components/goods/cate');
const params = () => import(/* webpackChunkName:"cate_params" */ '../components/goods/params');

const list = () => import(/* webpackChunkName:"list_add_edit" */ '../components/goods/list');
const add = () => import(/* webpackChunkName:"list_add_edit" */ '../components/goods/add');
const edit = () => import(/* webpackChunkName:"list_add_edit" */ '../components/goods/edit');

const order = () => import(/* webpackChunkName:"order_report" */ '../components/order/order');
const report = () => import(/* webpackChunkName:"order_report" */ '../components/report/report');
Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/index',
        component: index,
        redirect:'/welcome',
        children:[
            {
            path:'/welcome',
            component:welcome
            },
            {
             path: '/users',
             component:users    
            },
            {
                path:'/rights',
                component:rights 
            },
            {
                path:'/roles',
                component:roles
            },
            {
                path:'/categories',
                component:cate
            },
            {
                path:'/params',
                component:params
            },
            {
                path:'/goods',
                component:list
            },
            {
                path:'/goods/add',
                component:add
            },
            {
                path:'/goods/edit',
                component:edit
            },
            {
                path:'/orders',
                component:order
            },
            {
                path:'/reports',
                component:report
            }]
    }
];

const router = new VueRouter({
    base: process.env.BASE_URL,
    routes
});

//为路由对象添加beforeEach导航守卫
router.beforeEach((to, form, next) => {
    //如果用户访问的是登录页直接放行
    if (to.path === ('/login')) return next();
    const tokenStr = window.sessionStorage.getItem('token');
    //如果没有token，强制跳转到登录页
    if (!tokenStr) return next('/login');
    next()
});
export default router
